package com.yvtian.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yvtian.admin.dto.ReqRole;
import com.yvtian.admin.service.SysRoleService;
import com.yvtian.admin.vo.RoleVO;
import com.yvtian.annotation.Log;
import com.yvtian.annotation.PreAuthorize;
import com.yvtian.entity.Result;
import com.yvtian.entity.StatusCode;
import com.yvtian.enums.OperatorType;
import com.yvtian.utils.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Slf4j
@RestController
@RequestMapping(value = "/api/admin/role")
public class SysRoleController {

    @Autowired
    private SysRoleService roleService;

    @Log("查询角色详情")
    @PreAuthorize(hasPermission = "role:find")
    @RequestMapping(value = "/{roleId}", method = RequestMethod.GET)
    public Result findRoleById(@PathVariable("roleId") Integer roleId) {
        if (roleId == null) {
            return new Result(false, "id不能为空", StatusCode.PARAMS_IN_ERROR);
        }
        return new Result(true, "查询成功", StatusCode.OK, roleService.findRoleByRoleId(roleId));
    }

    /**
     * 添加角色
     *
     * @param reqRole 角色信息
     */
    @Log(value = "添加角色",isSave = true,operType = OperatorType.INSERT)
    @PreAuthorize(hasPermission = "role:add")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result addRole(@RequestBody ReqRole reqRole) {
        roleService.addRole(reqRole);
        return new Result(true, "添加成功", StatusCode.OK);
    }

    /**
     * 删除角色
     *
     * @param map ids：角色Id合集
     * @return
     */
    @Log(value = "删除角色",isSave = true,operType = OperatorType.DELETE)
    @PreAuthorize(hasPermission = "role:del")
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public Result delRole(@RequestBody Map<String, ArrayList<Integer>> map) {
        ArrayList<Integer> ids = map.get("ids");
        if (null == ids || ids.size() == 0) {
            return new Result(false, "id不能为空", StatusCode.PARAMS_IN_ERROR);
        }
        roleService.delRole(ids);
        return new Result(true, "删除成功", StatusCode.OK);
    }

    /**
     * 分页查询角色信息
     */
    @Log("分页查询角色")
    @PreAuthorize(hasPermission = "role:list")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result findByPage(Page page, RoleVO roleVO) {
        return new Result(true, "查询成功", StatusCode.OK, roleService.findByPage(page, roleVO));
    }

    /**
     * 修改角色信息
     *
     * @param map
     * @return
     */
    @Log(value = "修改角色信息",isSave = true,operType = OperatorType.UPDATE)
    @PreAuthorize(hasPermission = "role:update")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody Map<String, Object> map) {
        Integer roleId = (Integer) map.get("roleId");
        ArrayList<Integer> menuIds = (ArrayList<Integer>) map.get("menuIds");
        Map<String, Object> columns = (Map<String, Object>) map.get("columns");
        if (null == roleId) {
            return new Result(false, "角色Id不能为空", StatusCode.PARAMS_IN_ERROR);
        }
        roleService.updateRole(roleId, menuIds, columns);
        return new Result(true, "修改成功", StatusCode.OK);
    }

    /**
     * 修改角色状态
     *
     * @param map disable:状态0：启用，1停用 ,roleId:角色Id
     */
    @Log(value = "修改角色状态",isSave = true,operType = OperatorType.UPDATE)
    @PreAuthorize(hasPermission = "role:update")
    @RequestMapping(value = "/changeDisable", method = RequestMethod.POST)
    public Result changeDisable(@RequestBody Map<String, Object> map) {
        String disable = (String) map.get("disable");
        Integer roleId = (Integer) map.get("roleId");
        roleService.changeDisable(disable, roleId);
        return new Result(true, "修改成功", StatusCode.OK);
    }

//    /**
//     * 添加用户的权限
//     *
//     * @param map userId:用户Id，roleIds角色的Id合集
//     */
//    @PreAuthorize(hasPermission = "role:addUser")
//    @RequestMapping(value = "/update/userRole", method = RequestMethod.POST)
//    public Result updateRoleToUser(@RequestBody Map<String, Object> map) {
//        String userId = (String) map.get("userId");
//        ArrayList<Integer> roleIds = (ArrayList<Integer>) map.get("roleIds");
//        if (userId == null || userId.equals("")) {
//            return new Result(false, "用户Id不能为空", StatusCode.PARAMS_IN_ERROR);
//        }
//        if (null == roleIds || roleIds.size() == 0) {
//            return new Result(false, "角色Id不能为空", StatusCode.PARAMS_IN_ERROR);
//        }
//        roleService.updateRoleToUser(roleIds, userId);
//        return new Result(true, "修改成功", StatusCode.OK);
//    }

    /**
     * 分页获取拥有该角色的用户
     */
    @Log("分页获取拥有该角色的用户")
    @PreAuthorize(hasPermission = "role:findUser")
    @RequestMapping(value = "/find/user", method = RequestMethod.POST)
    public Result getUserByRole(@RequestBody Map<String, Object> map) {
        Integer roleId = (Integer) map.get("roleId");
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        String nickname = (String) map.get("nickname");
        String mobile = (String) map.get("mobile");
        if (roleId == null || roleId <= 0) {
            return new Result(false, "角色Id异常", StatusCode.PARAMS_IN_ERROR);
        }
        return new Result(true, "查询成功", StatusCode.OK, roleService.findUserByRole(roleId, pageNum, pageSize, nickname, mobile));
    }

    /**
     * 删除该用户的角色
     *
     * @param map roleId 角色id，userIds用户Id的集合
     */
    @Log(value = "删除该用户的角色",isSave = true,operType = OperatorType.DELETE)
    @PreAuthorize(hasPermission = "role:cancelAuth")
    @RequestMapping(value = "/cancelAuth", method = RequestMethod.POST)
    public Result cancelRoleAuth(@RequestBody Map<String, Object> map) {
        Integer roleId = (Integer) map.get("roleId");
        ArrayList<String> userIds = (ArrayList<String>) map.get("userIds");
        if (roleId == null || roleId <= 0) {
            return new Result(false, "角色Id异常", StatusCode.PARAMS_IN_ERROR);
        }
        if (userIds == null || userIds.size() <= 0) {
            return new Result(false, "用户Id异常", StatusCode.PARAMS_IN_ERROR);
        }
        roleService.delUserRoleByUserIds(roleId, userIds);
        return new Result(true, "删除成功", StatusCode.OK);
    }

    /**
     * 分页获取不拥有该权限的用户
     */
    @Log("分页获取不拥有该权限的用户")
    @PreAuthorize(hasPermission = "role:findUser")
    @RequestMapping(value = "/find/notUser", method = RequestMethod.POST)
    public Result getUserNotInRole(@RequestBody Map<String, Object> map) {
        String nickName = (String) map.get("nickname");
        String mobile = (String) map.get("mobile");
        Integer roleId = (Integer) map.get("roleId");
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        if (roleId == null || roleId <= 0) {
            return new Result(false, "角色Id异常", StatusCode.PARAMS_IN_ERROR);
        }
        return new Result(true, "查询成功", StatusCode.OK, roleService.findUserNotInRole(roleId, pageNum, pageSize, nickName, mobile));
    }

    /**
     * 为角色分配用户
     *
     * @param map userIds:用户Id的集合，roleId：角色Id
     */
    @Log(value = "为角色分配用户",isSave = true,operType = OperatorType.UPDATE)
    @PreAuthorize(hasPermission = "role:addUser")
    @RequestMapping(value = "/add/user", method = RequestMethod.POST)
    public Result addRoleToUsers(@RequestBody Map<String, Object> map) {
        ArrayList<String> userIds = (ArrayList<String>) map.get("userIds");
        Integer roleId = (Integer) map.get("roleId");
        if (roleId == null || roleId <= 0) {
            return new Result(false, "角色Id异常", StatusCode.PARAMS_IN_ERROR);
        }
        if (userIds == null || userIds.size() == 0) {
            return new Result(false, "用户Id不能为空", StatusCode.PARAMS_IN_ERROR);
        }
        roleService.addRoleToUser(roleId, userIds);
        return new Result(true, "添加成功", StatusCode.OK);
    }

    @Log(value = "下载角色所有的Excel",isSave = true,operType = OperatorType.EXPORT)
    @PreAuthorize(hasPermission = "role:export")
    @RequestMapping(value = "/export", method = RequestMethod.POST)
    public void getExcelForRoleAll(HttpServletResponse response) {
        List<List<String>> export = roleService.export();
        List<String> columnName = Arrays.asList(ExcelUtil.ROLE_COLUMN);
        ExcelUtil.uploadExcelAboutUser(response,"角色列表",columnName,export);
    }
}
